package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.ReviewDelegate;
import coms.delegates.LocationDelegate;
import coms.delegates.PublicationDelegate;
import coms.delegates.UserDelegate;
import coms.dto.Review;
import coms.dto.Location;
import coms.dto.Publication;
import coms.dto.User;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReviewAssignmentAction extends ComsActionSupport implements IAction {

    private int reviewId;
    private Review review;
    private List<Review> reviewList;
    private ReviewDelegate reviewDelegate;
    
    private List<Publication> publicationList;
    private PublicationDelegate publicationDelegate;
    
    private List<User> reviewerList;
    private UserDelegate userDelegate;
    private String action;
private boolean admin;

    public boolean isAdmin() {
         User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    public ReviewAssignmentAction() {
        review = new Review();
        
        reviewList = new ArrayList<Review>();
        reviewDelegate = new ReviewDelegate();

        publicationList = new ArrayList<Publication>();
        publicationDelegate = new PublicationDelegate();
        
        userDelegate = new UserDelegate();
        reviewerList = new ArrayList<User>();

    }

    private void initForm() {
        try {
            reviewerList = userDelegate.findAll();
            publicationList = publicationDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateDefault() {
        review = new Review();
      
    }

    private void populate() {
        try {
            review = reviewDelegate.getById(reviewId);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {
        initForm();
        populateDefault();
        action = "createReviewAssignment_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateReviewAssignment_" + review.getId();
        return "edit";
    }

    @Override
    public String create() {
        try {
            reviewDelegate.create(review);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
            reviewDelegate.update(review);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "updated";
    }

    @Override
    public String delete() {
        populate();
        try {
            reviewDelegate.delete(review);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            reviewList = reviewDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        initForm();
        try {
                reviewList = reviewDelegate.search(review);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "search";
    }

    public Review getReview() {
        return review;
    }

    public void setReview(Review review) {
        this.review = review;
    }

    public List<Review> getReviewList() {
        return reviewList;
    }

    public void setReviewList(List<Review> reviewList) {
        this.reviewList = reviewList;
    }

    /*
    public String getReviewer() {
       if (review!=null && review.getReviewer()!=null) 
           return "" +review.getReviewer().getId();
       else return "-1";
    }
   */

    public void setReviewer(int reviewerId) {
        try {
           User reviewer = userDelegate.getById(reviewerId);
            review.setReviewer(reviewer);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /*
    public String getPublication() {
       if (review!=null && review.getPublication()!=null) 
           return "" +review.getPublication().getId();
       else return "-1";
    }
   */
    
    public void setPublication(int publicationId) {
        try {
           Publication publication = publicationDelegate.getById(publicationId);
            review.setPublication(publication);
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getReviewId() {
        return "" + reviewId;
    }

    public void setReviewId(String reviewId) {
        try {
            if (reviewId != null && !reviewId.equals("")) {
                this.reviewId = Integer.parseInt(reviewId);
                review.setId(this.reviewId);
            }
        } catch (Exception ex) {
            Logger.getLogger(ReviewAssignmentAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public List<Publication> getPublicationList() {
        return publicationList;
    }

    public void setPublicationList(List<Publication> publicationList) {
        this.publicationList = publicationList;
    }

    public List<User> getReviewerList() {
        return reviewerList;
    }

    public void setReviewerList(List<User> reviewerList) {
        this.reviewerList = reviewerList;
    }
    
    
}
